package cn.itcast.bos.web.action.transit;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.transit.TransitInfo;
import cn.itcast.bos.service.transit.TransitInfoService;
import cn.itcast.bos.web.action.common.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class TransitInfoAction extends BaseAction<TransitInfo> {

	@Autowired
	private TransitInfoService transitInfoService;

	// 属性驱动接受需要开启运输配送的运单Id
	private String wayBillIds;

	public void setWayBillIds(String wayBillIds) {
		this.wayBillIds = wayBillIds;
	}

	// 在运单改管理页面中开启运单的中转配送
	@Action(value = "transit_create",
			results = { @Result(name = "success", type = "json") })
	public String create() {
		Map<String, Object> result = new HashMap<String, Object>();
		// 调用业务层,保存transitInfo信息
		try {
			// 保存成功
			transitInfoService.createTransit(wayBillIds);

			result.put("success", true);
			result.put("msg", "开启中转配送成功!");
		} catch (Exception e) {
			e.printStackTrace();
			// 保存失败
			result.put("success", false);
			result.put("msg", "开启中转配送失败,异常" + e.getMessage());
		}
		ActionContext.getContext().getValueStack().push(result);

		return SUCCESS;
	}

	// 运输信息列表分页查询
	@Action(value = "transitInfo_pageQuery",
			results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<TransitInfo> pageData = transitInfoService.pageQuery(pageable);
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}

}
